<template>
  <div class="account-settings-info-view">
    <a-row :gutter="16">
      <a-col :md="24" :lg="16">
        <a-form layout="vertical" :form="userForm">
          <a-form-item label="主键" style="display:none;">
            <a-input v-decorator="['id', { rules: [{ required: true }], initialValue: this.userFormData.id }]" />
          </a-form-item>
          <a-form-item label="昵称" :required="true">
            <a-input
              :maxLength="10"
              v-decorator="[
                'nickName',
                { rules: [{ required: true, message: '请输入昵称' }], initialValue: this.userFormData.nickName }
              ]"
              placeholder="请输入昵称"
            />
          </a-form-item>
          <a-form-item label="性别" :required="true">
            <a-select
              v-decorator="[
                'sex',
                {
                  rules: [{ required: false, message: '请选择用户的性别' }],
                  initialValue: this.$isNull(this.userFormData.sex) ? '' : this.userFormData.sex + ''
                }
              ]"
              placeholder="请选择用户的性别"
            >
              <a-select-option value="1">男</a-select-option>
              <a-select-option value="0">女</a-select-option>
            </a-select>
          </a-form-item>

          <a-form-item>
            <a-button type="primary" @click="updateUserInfo">保存</a-button>
          </a-form-item>
        </a-form>
      </a-col>
    </a-row>
  </div>
</template>

<script>
// import { mapGetters } from 'vuex'
import { updateUserInfo } from '@/api/system/user/user'
export default {
  components: {},
  data() {
    return {
      userForm: this.$form.createForm(this),
      userFormData: {}
    }
  },
  // 用于数据初始化
  created: function() {
    this.userFormData = {}
    // 获取用户信息
    this.$store.dispatch('GetInfo').then(res => {
      const data = res.data
      this.$store.commit('SET_NICK_NAME', { name: data.nickName })
      this.userFormData = {
        id: data.id,
        nickName: data.nickName,
        sex: data.sex
      }
    })
  },
  methods: {
    // 保存表单
    updateUserInfo(e) {
      e.preventDefault()
      this.userForm.validateFields((err, values) => {
        if (err) {
          return false
        }
        updateUserInfo(values)
          .then(res => {
            if (res.code !== 200) {
              this.$message.error(res.msg)
            } else {
               this.$store.commit('SET_NICK_NAME', { name: values.nickName })
              this.$message.success(res.msg)
            }
          })
          .catch(err => {
            console.log(err)
          })
        console.log(values)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.avatar-upload-wrapper {
  height: 200px;
  width: 100%;
}

.ant-upload-preview {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 180px;
  border-radius: 50%;
  box-shadow: 0 0 4px #ccc;

  .upload-icon {
    position: absolute;
    top: 0;
    right: 10px;
    font-size: 1.4rem;
    padding: 0.5rem;
    background: rgba(222, 221, 221, 0.7);
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.2);
  }
  .mask {
    opacity: 0;
    position: absolute;
    background: rgba(0, 0, 0, 0.4);
    cursor: pointer;
    transition: opacity 0.4s;

    &:hover {
      opacity: 1;
    }

    i {
      font-size: 2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -1rem;
      margin-top: -1rem;
      color: #d6d6d6;
    }
  }

  img,
  .mask {
    width: 100%;
    max-width: 180px;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
  }
}
</style>
